package com.shengde.jinwawaforleader.model.homePage.adapter;

import android.content.Context;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.bumptech.glide.Glide;
import com.shengde.jinwawaforleader.R;
import com.shengde.jinwawaforleader.network.BaseProtocol;
import com.shengde.jinwawaforleader.network.protocolbean.ProHappyListInfo;
import com.shengde.jinwawaforleader.utils.DateUtil;
import com.shengde.jinwawaforleader.views.CircleImageView;

import java.util.List;

/**
 * Created by Chason on 2016/12/22.
 */

public class HappyListAdapter extends RecyclerView.Adapter<RecyclerView.ViewHolder> {

    private Context context;

    private final int TEACHER_ITEM_TITLE = 0;
    private final int TEACHER_ITEM = 1;
    private final int STUDENT_ITEM_TITLE = 2;
    private final int STUDENT_ITEM = 3;

    private List<ProHappyListInfo.StuInfoEntity> stuInfo;
    private List<ProHappyListInfo.InfoEntity> info;

    public HappyListAdapter(Context context,List<ProHappyListInfo.StuInfoEntity> stuInfo,List<ProHappyListInfo.InfoEntity> info) {
        this.context = context;
        this.stuInfo=stuInfo;
        this.info=info;
    }

    public void resetList(List<ProHappyListInfo.StuInfoEntity> stuInfo,List<ProHappyListInfo.InfoEntity> info){
        this.stuInfo = null;
        this.info = null;
        this.stuInfo=stuInfo;
        this.info=info;
    }

    //获取数据的数量
    @Override
    public int getItemCount() {
        return stuInfo.size()+info.size()+2;
    }

    @Override
    public int getItemViewType(int position) {
        if(position==0) {
            return TEACHER_ITEM_TITLE;
        }else if(position<info.size()+1) {
            return TEACHER_ITEM;
        }else if(position==info.size()+1) {
            return STUDENT_ITEM_TITLE;
        }else {
            return STUDENT_ITEM;
        }
    }

    //创建新View，被LayoutManager所调用
    @Override
    public RecyclerView.ViewHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
        View view;
        ItemViewHolder itemViewHolder;
        TitleViewHolder titleViewHolder;
        switch (viewType) {
            case TEACHER_ITEM_TITLE:
            case STUDENT_ITEM_TITLE:
                view = LayoutInflater.from(context).inflate(R.layout.item_happylist_title,viewGroup,false);
                titleViewHolder = new TitleViewHolder(view);
                return titleViewHolder;
            case TEACHER_ITEM:
            case STUDENT_ITEM:
                view = LayoutInflater.from(context).inflate(R.layout.item_happylist,viewGroup,false);
                itemViewHolder = new ItemViewHolder(view);
                return  itemViewHolder;
        }
        return null;
    }
    //将数据与界面进行绑定的操作
    @Override
    public void onBindViewHolder(RecyclerView.ViewHolder viewHolder, int position) {
        switch (getItemViewType(position)) {
            case TEACHER_ITEM_TITLE:
                ((TitleViewHolder)viewHolder).tv_title.setText("请假老师 "+info.size()+"名");
                break;
            case TEACHER_ITEM:
                Glide.with(context).load(BaseProtocol.IMG_BASE+info.get(position-1).getLogo()).error(R.drawable.touxiang).override(160,160).into(((ItemViewHolder)viewHolder).civ_touxiang);
                ((ItemViewHolder)viewHolder).tv_name.setText(info.get(position-1).getName());
                ((ItemViewHolder)viewHolder).tv_cname_title.setText("所在部门：");
                ((ItemViewHolder)viewHolder).tv_cname.setText(info.get(position-1).getCname());
                ((ItemViewHolder)viewHolder).tv_tel.setText(info.get(position-1).getTel());
                ((ItemViewHolder)viewHolder).tv_reason.setText(info.get(position-1).getReason());
                ((ItemViewHolder)viewHolder).tv_date.setText(DateUtil.getDateY(info.get(position-1).getTime()+""));
                break;
            case STUDENT_ITEM_TITLE:
                ((TitleViewHolder)viewHolder).tv_title.setText("请假学生 "+stuInfo.size()+"名");
                break;
            case STUDENT_ITEM:
                Glide.with(context).load(BaseProtocol.IMG_BASE+stuInfo.get(position-2-info.size()).getLogo()).error(R.drawable.touxiang).override(160,160).into(((ItemViewHolder)viewHolder).civ_touxiang);
                ((ItemViewHolder)viewHolder).tv_name.setText(stuInfo.get(position-2-info.size()).getName());
                ((ItemViewHolder)viewHolder).tv_cname.setText(stuInfo.get(position-2-info.size()).getCname());
                ((ItemViewHolder)viewHolder).tv_tel.setText(stuInfo.get(position-2-info.size()).getTel());
                ((ItemViewHolder)viewHolder).tv_reason.setText(stuInfo.get(position-2-info.size()).getReason());
                ((ItemViewHolder)viewHolder).tv_date.setText(DateUtil.getDateY(stuInfo.get(position-2-info.size()).getTime()+""));
                break;
        }
    }

    //自定义的ViewHolder，持有每个Item的的所有界面元素
    public static class ItemViewHolder extends RecyclerView.ViewHolder {

        public CircleImageView civ_touxiang;
        public TextView tv_name;
        public TextView tv_cname_title;
        public TextView tv_cname;
        public TextView tv_tel;
        public TextView tv_reason;
        public TextView tv_date;

        public ItemViewHolder(View view){
            super(view);
            civ_touxiang = (CircleImageView) view.findViewById(R.id.civ_touxiang);
            tv_name = (TextView) view.findViewById(R.id.tv_name);
            tv_cname_title = (TextView) view.findViewById(R.id.tv_cname_title);
            tv_cname = (TextView) view.findViewById(R.id.tv_cname);
            tv_tel = (TextView) view.findViewById(R.id.tv_tel);
            tv_reason = (TextView) view.findViewById(R.id.tv_reason);
            tv_date = (TextView) view.findViewById(R.id.tv_date);
        }
    }

    public static class TitleViewHolder extends RecyclerView.ViewHolder {

        public TextView tv_title;

        public TitleViewHolder(View view){
            super(view);
            tv_title = (TextView) view.findViewById(R.id.tv_title);
        }

    }
}
